/*
	Copyright 2008, 2009, 2010 Brightworks, Inc.
	
	This file is part of Language Mentor.
	
	Language Mentor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Language Mentor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.controller.audio
{
    import com.langcollab.languagementor.vo.ChunkFileVO;
    
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.SecurityErrorEvent;

    [Event(name="ioErrorReport", type="com.langcollab.languagementor.controller.audio.AudioProgressEvent")]

    public class AudioSequenceLeaf_File extends AudioSequenceLeaf
    {
		public var chunkFileVO:ChunkFileVO;
        public var url:String;

        public function AudioSequenceLeaf_File()
        {
            super();
        }

        // ****************************************************
        //
        //          Public Methods
        //
        // ****************************************************

        public override function dispose():void
        {
            removeEventListeners();
        }

        public override function start():void
        {
            super.start();
            AudioPlayer.getInstance().addEventListener(Event.SOUND_COMPLETE, onElementComplete);
            AudioPlayer.getInstance().addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            AudioPlayer.getInstance().addEventListener(SecurityErrorEvent.SECURITY_ERROR, onIOError);
            AudioPlayer.getInstance().play(url, chunkFileVO.maxVolume);
        }

        public override function stop():void
        {
            super.stop();
            removeEventListeners();
            AudioPlayer.getInstance().stop();
        }

        // ****************************************************
        //
        //          Protected Methods
        //
        // ****************************************************

        protected override function onElementComplete(event:Event):void
        {
            removeEventListeners();
            super.onElementComplete(event);
        }

        // ****************************************************
        //
        //          Private Methods
        //
        // ****************************************************

        private function onIOError(event:ErrorEvent):void
        {
            // Dispatch IOERROR_REPORT - AudioController listens for this
            dispatchEvent(new AudioProgressEvent(AudioProgressEvent.IOERROR_REPORT, id, levelId, url, event.text));
        }

        private function removeEventListeners():void
        {
            AudioPlayer.getInstance().removeEventListener(Event.SOUND_COMPLETE, onElementComplete);
            AudioPlayer.getInstance().removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
            AudioPlayer.getInstance().removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onIOError);
        }
    }
}